﻿<ServiceContract()> _
Public Interface IUpdateServer

    <OperationContract()> _
    Function NeedUpdate(ByVal Version As Version, ByVal dotNetVersion As Double) As Boolean

    <OperationContract()> _
    Function GetFiles(ByVal dotNetVersion As Double) As List(Of String)

    <OperationContract()> _
    Function GetFileLength(ByVal dotNetVersion As Double, ByVal Index As Integer) As Long

    <OperationContract()> _
    Function GetFile(ByVal dotNetVersion As Double, ByVal Index As Integer) As IO.Stream
End Interface

<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple, InstanceContextMode:=InstanceContextMode.Single)> _
Public Class UpdateServer
    Implements IUpdateServer

    Dim dotNet35Version As Version
    Dim dotNet40Version As Version

    Public Sub New()
        Try
            dotNet35Version = New Version(System.Diagnostics.FileVersionInfo.GetVersionInfo("ClientBin\3.5\MPQClient35.exe").ProductVersion)
            dotNet40Version = New Version(System.Diagnostics.FileVersionInfo.GetVersionInfo("ClientBin\4.0\MPQClient.exe").ProductVersion)
        Catch
        End Try
    End Sub

    Public Function NeedUpdate(ByVal Version As System.Version, ByVal dotNetVersion As Double) As Boolean Implements IUpdateServer.NeedUpdate
        Select Case dotNetVersion
            Case 3.5
                Return Version <> dotNet35Version
            Case 4.0
                Return Version <> dotNet40Version
            Case Else
                Return False
        End Select
    End Function

    Public Function GetFile(ByVal dotNetVersion As Double, ByVal Index As Integer) As IO.Stream Implements IUpdateServer.GetFile
        Dim FS As New IO.FileStream(IO.Directory.GetFiles("ClientBin\" & dotNetVersion.ToString("F1", Globalization.CultureInfo.InvariantCulture) & "\")(Index), IO.FileMode.Open)
        'Dim Buffer(FS.Length) As Byte
        'FS.Read(Buffer, 0, FS.Length)
        'Dim MS As New IO.MemoryStream()
        'MS.Write(Buffer, 0, FS.Length)
        'FS.Close()
        'MS.Position = 0
        Return CType(FS, IO.Stream)
    End Function

    Public Function GetFileLength(ByVal dotNetVersion As Double, ByVal Index As Integer) As Long Implements IUpdateServer.GetFileLength
        Dim FS As New IO.FileStream(IO.Directory.GetFiles("ClientBin\" & dotNetVersion.ToString("F1", Globalization.CultureInfo.InvariantCulture) & "\")(Index), IO.FileMode.Open)
        Dim Length As Long = FS.Length
        FS.Close()
        Return Length
    End Function

    Public Function GetFiles(ByVal dotNetVersion As Double) As List(Of String) Implements IUpdateServer.GetFiles
        Return IO.Directory.GetFiles("ClientBin\" & dotNetVersion.ToString("F1", Globalization.CultureInfo.InvariantCulture) & "\").ToList
    End Function
End Class